<?php
namespace wechat;

//获取TOKEN
class Token extends Base{
	
	/**
	* 获取普通access token  2017-10-15
	*/
	public function gettoken($appid,$appsecret,$attach = array()){		
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";	
		$result = https_request($url);
		
		!isset($attach['uid']) && $attach['uid'] = 0;		
		apilog($attach['uid'],'wechat','getaccesstoken',$url,'',$result);
		
		return $result;
	}
	

	/**
	* 通过code换取网页授权access_token  2017-10-15
	*/
	public function getauthtoken($appid,$appsecret,$code,$attach = array()){
		$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";
		$result = https_request($url);
		
		!isset($attach['uid']) && $attach['uid'] = 0;		
		apilog($attach['uid'],'wechat','getauthtoken',$url,'',$result);
		
		return $result;
	}
	
	/**
	* 获取普通jsapi_ticket 临时票据  2017-10-15
	*/
	public function getjsapi($access_token,$attach = array()){
		$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$access_token&type=jsapi";
		//$url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";//如果是企业号用以下 URL 获取 ticket
		$result = https_request($url);
		
		!isset($attach['uid']) && $attach['uid'] = 0;
		apilog($attach['uid'],'wechat','getjsapi',$url,'',$result);
		
		return $result;
	}
		
	/**
	* 生成signpackage JSSDK分享用	2017-10-15
	*/
	public function getsignature($jsapiTicket) {		
		$url = get_url();
		$timestamp = time();
		$nonceStr = createnoncestr();
		$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
		$signature = sha1($string);
		$signPackage = array(
			'nonceStr'  => $nonceStr,
			'timestamp' => $timestamp,
			'url'       => $url,
			'signature' => $signature,
			'rawString' => $string
		);
		return $signPackage;
	}
}